package com.example.meetsmart.service;

import com.example.meetsmart.model.Room;
import com.example.meetsmart.repository.RoomRepository;
import com.example.meetsmart.exception.ResourceNotFoundException;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoomService {
    private final RoomRepository roomRepository;

    public RoomService(RoomRepository roomRepository) {
        this.roomRepository = roomRepository;
    }

    @Transactional(readOnly = true)
    public List<Room> getAllRooms(Pageable pageable) {
        return roomRepository.findAll();
    }

    @Transactional(readOnly = true)
    public List<Room> getActiveRooms(Pageable pageable) {
        return roomRepository.findByIsActiveTrue();
    }

    @Transactional(readOnly = true)
    public Room getRoomById(Long id) {
        return roomRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Room not found with id: " + id));
    }

    @Transactional
    public Room createRoom(Room room) {
        return roomRepository.save(room);
    }

    @Transactional
    public Room updateRoom(Long id, Room roomDetails) {
        Room room = roomRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Room not found with id: " + id));

        if (roomDetails.getName() != null) {
            room.setName(roomDetails.getName());
        }
        if (roomDetails.getDescription() != null) {
            room.setDescription(roomDetails.getDescription());
        }
        if (roomDetails.getCapacity() != null) {
            room.setCapacity(roomDetails.getCapacity());
        }
        if (roomDetails.getHasProjector() != null) {
            room.setHasProjector(roomDetails.getHasProjector());
        }
        if (roomDetails.getHasWhiteboard() != null) {
            room.setHasWhiteboard(roomDetails.getHasWhiteboard());
        }
        if (roomDetails.getHasAudioSystem() != null) {
            room.setHasAudioSystem(roomDetails.getHasAudioSystem());
        }
        if (roomDetails.getIsActive() != null) {
            room.setIsActive(roomDetails.getIsActive());
        }

        return roomRepository.save(room);
    }

    @Transactional
    public void deleteRoom(Long id) {
        Room room = roomRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("Room not found with id: " + id));
        roomRepository.delete(room);
    }

    @Transactional(readOnly = true)
    public List<Room> findAvailableRoomsWithCriteria(int capacity, boolean hasProjector,
                                                     boolean hasWhiteboard, boolean hasAudioSystem) {
        return roomRepository.findAvailableRoomsWithCriteria(capacity, hasProjector, hasWhiteboard, hasAudioSystem);
    }

    @Transactional(readOnly = true)
    public List<Room> findAvailableRoomsBetweenTimes(LocalDateTime startTime, LocalDateTime endTime) {
        return roomRepository.findAvailableRoomsBetweenTimes(startTime, endTime);
    }
}